qx.Class.define("secretcuz.ui.BlogPostManagePage", {
	extend : qx.ui.tabview.Page,
	
	construct : function() {
		this.base(arguments, "Manage", "secretcuz/icons/list.png");
		this.setLayout(new qx.ui.layout.VBox());
		
		this.add(this._createControls());
		this.add(this._createTable());
	},
	
	members : {
		_table : null,
		_tableModel : null,
		_btnPublish : null,
		_btnDel : null,
		_btnEdit : null,
		
		_createTable : function() {
			this._tableModel = new qx.ui.table.model.Filtered();
			this._tableModel.setColumns([ "ID", "Title", "Date", "Meta"]);
			
			this._tableModel.setColumnEditable(1, true);
			this._tableModel.setColumnEditable(2, true);
			this._tableModel.setColumnEditable(3, true);

			this._table = new qx.ui.table.Table(this._tableModel);
			//this._table.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
			this._table.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION);
			this._table.setColumnWidth(0, 60);
			this._table.setColumnWidth(1, 100);
			this._table.setColumnWidth(2, 140);
			this._table.addListener("dataEdited", function(e) {
				var tableData = e.getData();
				var id = this._tableModel.getValue(0, tableData.row);
				var data = {
					title: this._tableModel.getValue(1, tableData.row),
					date: this._tableModel.getValue(2, tableData.row),
					meta: this._tableModel.getValue(3, tableData.row)
				};
				var bpRest = new secretcuz.io.rest.BlogPostResource();
				bpRest.addListener("success", function(e) {
					this.populate();
				}, this);
				bpRest.update({id: id}, data);
			}, this);
			
			this._table.getSelectionModel().addListener("changeSelection", function(e) {
				var selCount = this._table.getSelectionModel().getSelectedCount();
				if (selCount == 1) {
					this._btnEdit.setEnabled(true);
					this._btnDel.setEnabled(true);
				} else {
					if (selCount > 1) {
						this._btnDel.setEnabled(true);
					} else {
						this._btnDel.setEnabled(false);
					}
					this._btnEdit.setEnabled(false);
				}
			}, this);
			
			return this._table;
		},

		_createControls : function() {
			var bar = new qx.ui.toolbar.ToolBar();
			
			this._btnPublish = new qx.ui.toolbar.Button("Publish");
			bar.add(this._btnPublish);
			this._btnPublish.addListener("execute", function(e){
				var data = {
					title: "You has created me",
					content: "<p>Edit me please!</p>",
					date: new Date(),
					meta: "7inpham"
				}
				var bpRest = new secretcuz.io.rest.BlogPostResource();
				bpRest.addListener("success", function(e) {
					this.populate();
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #66CD00;'>Added a quick-post.</b>");
				}, this);
				bpRest.insert({}, data); // JSON.stringify
			}, this);
			
			this._btnEdit = new qx.ui.toolbar.Button("Edit");
			this._btnEdit.setEnabled(false);
			bar.add(this._btnEdit);
			this._btnEdit.addListener("execute", function(e) {
				var selRow = this._table.getSelectionModel().getAnchorSelectionIndex();
				if (selRow >= 0) {
					var id = this._tableModel.getValue(0, selRow);
					if (id != null) {
						this.loadProfileToEdit(id);	
					}
				}
			}, this);
			
			this._btnDel = new qx.ui.toolbar.Button("Delete");
			this._btnDel.setEnabled(false);
			bar.add(this._btnDel);
			this._btnDel.addListener("execute", function(e) {
				var selIds	= [];
				for (var x = 0; x < this._tableModel.getRowCount(); x++) {
					if (this._table.getSelectionModel().isSelectedIndex(x) == true) {
						selIds.push(Number(this._tableModel.getValue(0, x)));
					}
				}
				var bpRest = new secretcuz.io.rest.BlogPostResource();
				bpRest.addListener("success", function(e) {
					this.populate();
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #66CD00;'>Deleted.</b>");
				}, this);
				bpRest.addListener("error", function(e) {
					var app = qx.core.Init.getApplication();
					app.infoText.setValue("<b style='background-color: #fff; padding: 10px 15px; border: 1px solid #C5C5C5; color: #CC0000;'>Failed to delete a post.</b>");
				}, this);
				if (selIds.length > 0) {
					for (var i = 0; i < selIds.length; i++) {
						bpRest.remove({id: selIds[i]});
					}
				}
			}, this);

			return bar;
		},
		
		populate : function() {
			var bpRest = new secretcuz.io.rest.BlogPostResource();
			bpRest.addListener("success", function(e) {
				var tableData = [];
				var rawData = e.getData().items;
				for (var row = 0; row < rawData.length; row++) {
					tableData.push(
						[rawData[row].id,
						 rawData[row].title,
						 rawData[row].date,
						 rawData[row].meta
						 ]);
				}
				this._tableModel.setData(tableData);
			}, this);
			var data = {
				cursor : null,
				limit : 0
			};
			bpRest.list({}, data);
		},
		
		loadProfileToEdit : function(id) {
			var app = qx.core.Init.getApplication();
			app.tabView.setSelection(app.tabView.getChildren()[0]);
			app.bpProfilePage.populate(id);
		}
	}
});
